#include "application.h"

#define STORAGE_SERVICE_QUEUE_MAX			64
#define STORAGE_SERVICE_QUEUE_MAX_MSG_LEN	128

extern INT8U	device_plug_phase;
MSG_Q_ID	StorageServiceQ;
OS_EVENT	*StorageServiceMsgAck;

void task_storage_service_init(void)
{
	StorageServiceQ = msgQCreate(STORAGE_SERVICE_QUEUE_MAX, STORAGE_SERVICE_QUEUE_MAX, STORAGE_SERVICE_QUEUE_MAX_MSG_LEN);
	StorageServiceMsgAck = OSMboxCreate(NULL);

	ap_storage_service_init();

	DBG_PRINT("task_storage_service_init OK!\r\n");
}

void task_storage_service_entry(void *para)
{
	INT8U	msg_para[STORAGE_SERVICE_QUEUE_MAX_MSG_LEN];
	INT32U	msg_id;

	task_storage_service_init();

	DBG_PRINT("%s ... \r\n", __func__);

	while(1)
	{
		if(msgQReceive(StorageServiceQ, &msg_id, msg_para, STORAGE_SERVICE_QUEUE_MAX_MSG_LEN) == STATUS_FAIL)
		{
			continue;
		}

		switch(msg_id)
		{
		case MSG_STORAGE_SERVICE_STORAGE_CHECK:
			//DBG_PRINT(" s_usbd_pin = 0x%x\r\n",s_usbd_pin);
			DBG_PRINT("MSG_STORAGE_SERVICE_STORAGE_CHECK\r\n");
			if(!s_usbd_pin)
			{
				ap_storage_service_storage_mount();
			}
 
			break;

		case MSG_STORAGE_SERVICE_USB_IN:
			ap_storage_service_usb_plug_in();
			break;

		#if C_AUTO_DEL_FILE && (C_FIX_AVI_SIZE == CUSTOM_OFF)
		case MSG_STORAGE_SERVICE_FREESIZE_CHECK:
			ap_storage_service_freesize_check_and_del();
			break;

		case MSG_STORAGE_SERVICE_AUTODEL_LOCK:
			ap_storage_service_freesize_check_switch(FALSE);
			OSMboxPost(StorageServiceMsgAck, (void *) C_ACK_SUCCESS);
			break;

		case MSG_STORAGE_SERVICE_AUTODEL_UNLOCK:
			ap_storage_service_freesize_check_switch(TRUE);
			OSMboxPost(StorageServiceMsgAck, (void *) C_ACK_SUCCESS);
			break;
		#endif // #if C_AUTO_DEL_FILE && (C_FIX_AVI_SIZE == CUSTOM_OFF)

		case MSG_STORAGE_SERVICE_VIDEO_FILE_DEL:
			ap_storage_service_file_del();
			break;

		case MSG_STORAGE_SERVICE_FILE_DEL_ALL:
			ap_storage_service_file_delete_all();
			break;

		case MSG_STORAGE_SERVICE_LOCK_ONE:
			ap_storage_service_file_lock_one();
			break;

		case MSG_STORAGE_SERVICE_LOCK_ALL:
			ap_storage_service_file_lock_all();
			break;
		case MSG_STORAGE_SERVICE_UNLOCK_ALL:
			ap_storage_service_file_unlock_all();
			break;

		case MSG_STORAGE_SERVICE_UNLOCK_ONE:
			ap_storage_service_file_unlock_one();
			break;

		case STR_UNLOCK_ALL:
			ap_storage_service_file_unlock_all();
			break;

		case MSG_STORAGE_SERVICE_TIMER_START:
			
			ap_storage_service_timer_start();
			break;

		case MSG_STORAGE_SERVICE_TIMER_STOP:
			ap_storage_service_timer_stop();
			break;

		case MSG_STORAGE_SERVICE_AUD_REQ:
		case MSG_STORAGE_SERVICE_PIC_REQ:
		case MSG_STORAGE_SERVICE_PIC_REQ2:
		case MSG_STORAGE_SERVICE_VID_REQ:
			ap_storage_service_file_open_handle(msg_id, *(INT16U *) msg_para);
			break;

#if C_CYCLIC_VIDEO_RECORD == CUSTOM_ON

		case MSG_STORAGE_SERVICE_VID_CYCLIC_REQ:
			ap_storage_service_cyclic_record_file_open_handle();
			break;
#endif

		case MSG_STORAGE_SERVICE_THUMBNAIL_REQ:
		case MSG_STORAGE_SERVICE_BROWSE_REQ:
			ap_storage_service_play_req(*(INT32U *) msg_para);
			break;

		case MSG_STORAGE_SERVICE_FORMAT_REQ:

			
			ap_storage_service_format_req();
			break;

		case MSG_STORAGE_USBD_EXIT:
		case MSG_STORAGE_USBD_PCAM_EXIT:
			device_plug_phase = 0;

			//usbd_storage_exit = 1;
			disk_safe_exit(STORAGE_DEVICE);
			break;
		case MSG_STORAGE_SERVICE_WRITE_FILE:
			DBG_PRINT("MSG_STORAGE_SERVICE_WRITE_FILE\r\n");
			ap_storage_service_write_file_process();
			break;
		case MSG_STORAGE_SERVICE_CLOSE_FILE:
			DBG_PRINT("MSG_STORAGE_SERVICE_CLOSE_FILE\r\n");
			ap_storage_service_close_file_process();
			break;

#if C_RTC_REBOOT
		case MSG_SET_ALARM:
			ap_peripheral_rtc_alarm_set();
			break;
#endif

		default:
			break;
		}
	}
}
